package edu.gatech.cs2340.sharks.fms.data;

/**
 * TagAdapter.java
 * @author Miri Baker
 * 
 * Used to adapt an array of tags to a format 
 * that can be displayed as a list row
 */

import edu.gatech.cs2340.sharks.fms.R;
import android.app.Activity;
import android.content.Context;
import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class TagAdapter extends ArrayAdapter<Tag>
{
	/**
	 * The context from which we are being called
	 */
	private Context context;
	
	/**
	 * The resource ID of the current layout
	 */
	private int layoutResourceId;
	
	/**
	 * An array of tags to convert
	 */
	private Tag data[]; 
	
	public TagAdapter(Context context, int layoutResourceId, Tag[] data)
	{
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = data;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View row = convertView;
		TagHolder holder;
        
		if(row == null)
		{
		    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		    row = inflater.inflate(layoutResourceId, null);

		    holder = new TagHolder();
		    holder.tagName = (TextView)row.findViewById(R.id.tagName);
		    holder.edit = (Button)row.findViewById(R.id.editTag); 

		    row.setTag(holder);
		}

		else holder = (TagHolder)row.getTag();

		Tag tag = data[position];
		holder.tagName.setText(tag.name);
		holder.edit.setTag(tag.id);  

		return row;
	}
	
	/**
	 * Provides variables for the TextView and button for each
	 * listed tag to be set.
	 * 
	 * @author Miri
	 */
	static class TagHolder
	{
	    TextView tagName;
	    Button edit;
	}
}
